package model

import (
	"github.com/jinzhu/gorm"
	"time"
)

type Position struct {
	Id int64 `xorm:"pk autoincr"`
	Type int
	Name string
	CompanyId int64
	CompanyName string
	Requirement string
	DeliveredQuantity int
	Close bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

//func (position *Position)QueryPosition(pId int64, session *xorm.Session) (*Position, error) {
//
//	var p Position
//
//	_, gErr := session.ID(pId).Get(&p)
//	if gErr != nil{
//		return nil, gErr
//	}
//
//	return &p, nil
//}
//
//func (position *Position)UpdatePosition(pId int64, session *xorm.Session) error {
//
//	p, qErr := position.QueryPosition(pId, session)
//	if qErr != nil{
//		return qErr
//	}
//
//	p.DeliveredQuantity += 1
//	_, uErr:= session.ID(p.Id).Update(p)
//	if uErr != nil{
//		return uErr
//	}
//
//	return nil
//}


func (position *Position)QueryPosition(pId int64, tx *gorm.DB) (*Position, error) {

	var p Position

	gErr := tx.First(&p, pId).Error
	if gErr != nil{
		return nil, gErr
	}

	return &p ,nil
}

func (position *Position)UpdatePosition(pId int64, tx *gorm.DB) error {

	p, qErr := position.QueryPosition(pId, tx)
	if qErr != nil{
		return qErr
	}

	uErr := tx.Model(p).Update("delivered_quantity", p.DeliveredQuantity+1).Error
	if uErr != nil{
		return uErr
	}

	return nil
}